package cn.xueden.common.handler;

import cn.xueden.util.DateTimeUtil;
import cn.xueden.util.SecurityUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;

import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Objects;

/**mybatisPlus公共字段填充处理器（元数据对象处理器）
 * @author:梁志杰
 * @date:2025/6/8
 * @description:cn.xueden.common.handler
 * @version:1.0
 */
@Component
@Slf4j
public class FiledFullHandler implements MetaObjectHandler {

    /**
     * 新增数据填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        //没有创建人id就给他自动填充 放属性名而不是字段名
        Class<?> clazz = metaObject.getOriginalObject().getClass();
        Field[]  fields = clazz.getDeclaredFields();
        Arrays.stream(fields).forEach(field -> {
            /**
             * 填充创建人
             */
            if("userId".equals(field.getName())&&(Objects.isNull(getFieldValByName("userId",metaObject)))){
                log.info("user_id字段满足公共字段自动填充规则，已填充");
               this.strictInsertFill(metaObject,"userId",Integer.class, SecurityUtil.getUserId());
            }
            // 填充创建时间
            if("createTime".equals(field.getName())&&(Objects.isNull(getFieldValByName("createTime",metaObject)))){
                log.info("create_time字段满足公共字段自动填充规则，已填充");
                this.strictInsertFill(metaObject,"createTime", LocalDateTime.class, DateTimeUtil.getDateTime());
            }
            // 填充更新时间
            if("updateTime".equals(field.getName())&&(Objects.isNull(getFieldValByName("updateTime",metaObject)))){
                log.info("update_time字段满足公共字段自动填充规则，已填充");
                this.strictInsertFill(metaObject,"updateTime", LocalDateTime.class, DateTimeUtil.getDateTime());
            }
        });
    }

    /**
     * 更新填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Class<?> clazz = metaObject.getOriginalObject().getClass();
        Field[] fields = clazz.getDeclaredFields();
        Arrays.stream(fields).forEach(field -> {
            //填充更新时间
            if ("updateTime".equals(field.getName()) && (Objects.isNull(getFieldValByName("updateTime", metaObject)))) {
                log.info("update_time字段满足公共字段自动填充规则，已填充");
                this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, DateTimeUtil.getDateTime());
            }
        });
    }
}
